<template>
    <el-dialog title="配件选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    center
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配件编码" prop="accessoryCode">
        <el-input
          v-model="queryParams.accessoryCode"
          placeholder="请输入配件编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配件名称" prop="accessoryName">
        <el-input
          v-model="queryParams.accessoryName"
          placeholder="请输入配件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>




    <el-table v-loading="loading" :data="accessoryList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
        <el-table-column width="55" align="center" >
            <template v-slot="scope">
                <el-radio v-model="selectedAccessoryId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
            </template>
        </el-table-column>
        <el-table-column label="配件编码" width="100" prop="accessoryCode" >
        </el-table-column>
        <el-table-column label="配件名称" width="200" align="center" prop="accessoryName" :show-overflow-tooltip="true"/>
        <el-table-column label="规格型号" width="100" align="center" prop="specification" :show-overflow-tooltip="true"/>
        <el-table-column label="所属分类" width="200" align="center" prop="accessoryTypeCode" :show-overflow-tooltip="true"/>
        <el-table-column label="单位" width="50" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"/>
        <el-table-column label="总库存" width="80" align="center" prop="totalInventory" :show-overflow-tooltip="true"/>
        <el-table-column label="剩余库存" width="80" align="center" prop="remainInventory" :show-overflow-tooltip="true"/>


      <!--        <el-table-column label="工单来源" align="center" prop="orderSource" >
                  <template slot-scope="scope">
                      <dict-tag :options="dict.type.mes_workorder_sourcetype" :value="scope.row.orderSource"/>
                  </template>
              </el-table-column>-->
<!--        <el-table-column label="需求日期" align="center" prop="requestDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>

</template>
<script>
    // import { listWorkorder} from "@/api/mes/pro/workorder";
    import request from "@/utils/request";

    export default {
        name: 'WorkOrderSelectSingle',
        components: {},
        // dicts: ['mes_order_status','mes_workorder_sourcetype'],
        props:{
          workorder: {
            type: Object,
            default: function(){
              return {'workorderType': 'SELF'}
            }} //外部传入的工单过滤信息
        },
        data() {
            return {
                showFlag: false,
                // 遮罩层
                loading: true,
                // 选中数组
                selectedAccessoryId: undefined,
                selectedRows: [],
                 // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                 // 生产工单表格数据
                accessoryList: [],
                 // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    accessoryCode: null,
                    accessoryName: null,
                    deleted: 0,
                },

            }
        },
        created() {
            this.getList();
        },
        methods:{
            /** 查询生产工单列表 */
            getList() {
                this.loading = true;
                listAccessory(this.queryParams).then(response => {
                    this.accessoryList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            handleCurrent(row){
                if(row){
                    this.selectedRows = row;
                }
            },
            // 单选选中数据
            handleRowChange(row) {
                if(row){
                    this.selectedRows = row;
                }
            },
            //双击选中
            handleRowDbClick(row){
                if(row){
                    this.selectedRows = row;
                    this.$emit('onSelected',this.selectedRows);
                    this.showFlag = false;
                }
            },
            //确定选中
            confirmSelect(){
                if(this.selectedAccessoryId == null || this.selectedAccessoryId == 0){
                    this.$notify({
                        title:'提示',
                        type:'warning',
                        message: '请至少选择一条数据!'
                    });
                    return;
                }
                this.$emit('onSelected',this.selectedRows);
                this.showFlag = false;
            }
        }
    }

    export function listAccessory(query) {
        return request({
          url: '/md/accessory/list',
          method: 'get',
          params: query
        })
      }
</script>
